import json

import time
from selenium import webdriver

from const import origin_url


driver = webdriver.Chrome()

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """
    Object.defineProperty(navigator, 'webdriver', {
      get: () => undefined
    })
  """
})


driver.get(origin_url)


load_dict = {}
with open("links.json", 'r', encoding="utf-8") as load_f:
    load_dict = json.load(load_f)


for link in load_dict:
    driver.get(link[2])
    elem = driver.find_element_by_xpath("//*")
    source_code = elem.get_attribute("outerHTML")

    with open('./pages/{}-{}.html'.format(link[0],
                                          link[1].replace(" ", "-")), 'wb') as f:
        f.write(source_code.encode('gb2312'))
    time.sleep(2)

driver.close()
